import tkinter as tk
from PIL import Image, ImageTk

import statistics

# 示例调查数据
survey_data = [
    {
        "question": "你对课程的满意度如何？",
        "options": ["1", "2", "3", "4", "5"],
        "responses": ["1", "3", "4", "2", "5", "3", "4", "5", "2", "1"]
    },
    {
        "question": "你每天学习多长时间？（小时）",
        "options": ["1", "2", "3", "4", "5"],
        "responses": ["1", "2", "3", "3", "2", "4", "5", "3", "4", "2"]
    },
    {
        "question": "你最喜欢的颜色是什么？",
        "options": ["红色", "蓝色", "绿色", "黄色", "紫色"],
        "responses": ["红色", "蓝色", "绿色", "黄色", "红色", "蓝色", "绿色", "紫色", "红色", "蓝色"]
    }
]

class Display(tk.Toplevel):
    def __init__(self, parent):
        super().__init__()

        self.parent = parent
        self.title("问卷详情")

        # 获取屏幕的宽和高
        sreen_width = self.winfo_screenwidth()
        screen_height = self.winfo_screenheight()

        # 设置窗口的宽和高
        root_width = int(sreen_width / 2)
        root_height = int(screen_height / 2)

        # 使窗口居中
        self.geometry("{}x{}+{}+{}".format(
            root_width, root_height,
            int((sreen_width - root_width) / 2), int((screen_height - root_height) / 2)
        ))

        # 使用 Pillow 加载图像
        image = Image.open("Image/return.png")
        # 调整图像大小
        new_size = (25, 25)  # 设置新的尺寸 (宽度, 高度)
        resized_image = image.resize(new_size, Image.LANCZOS)
        photo = ImageTk.PhotoImage(resized_image)

        self.back_button = tk.Button(self,image=photo ,text='返回',font=("宋体", 15), compound='left',command=self.back_to_main)
        self.back_button.photo = photo  # 保存图像对象的引用
        self.back_button.place(relx=0.01,rely=0.01,relwidth=0.15,relheight=0.1)

        report_text = tk.Text(self)
        report_text.place(relx=0.01,rely=0.15,relwidth=0.97,relheight=0.85)

        for i, data in enumerate(survey_data):
            question = data["question"]
            options = data["options"]
            responses = data["responses"]

            report_text.insert(tk.END, f"问题 {i + 1}: {question}\n")
            if responses:
                total_responses = len(responses)
                option_counts = {option: responses.count(option) for option in options}

                for option, count in option_counts.items():
                    percentage = (count / total_responses) * 100
                    report_text.insert(tk.END, f"{option}: {count} 次 ({percentage:.2f}%)\n")

                try:
                    numeric_responses = list(map(float, responses))
                    mean_value = statistics.mean(numeric_responses)
                    median_value = statistics.median(numeric_responses)
                    variance_value = statistics.variance(numeric_responses)

                    report_text.insert(tk.END, f"平均值: {mean_value:.2f}\n")
                    report_text.insert(tk.END, f"中位数: {median_value:.2f}\n")
                    report_text.insert(tk.END, f"方差: {variance_value:.2f}\n")
                except ValueError:
                    report_text.insert(tk.END, "选项内容不是数字，无法计算平均值、中位数和方差。\n")
            else:
                report_text.insert(tk.END, "没有收集到任何响应。\n")

            report_text.insert(tk.END, "\n")




    def back_to_main(self):
        self.destroy()
        self.parent.deiconify()